package jp.joogoo.web.controller;

import jp.joogoo.web.meta.M_serviceMeta;
import jp.joogoo.web.model.M_service;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.repackaged.org.json.JSONObject;

public class TestController extends Controller {

    private static final M_serviceMeta meta = new M_serviceMeta();
    
    @Override
    protected Navigation run() throws Exception {
        if (Datastore.query(meta).filter("serviceName", FilterOperator.EQUAL, "サイボウズLive").count() == 0) {
            M_service service = new M_service();
            service.setServiceName("サイボウズLive");
            { // service provider settings.
                // see <https://developer.cybozulive.com/doc/current/pub/authorize.html>
                // see <https://developer.cybozulive.com/doc/current/pub/notification.html>
                JSONObject serviceProvider = new JSONObject();
                serviceProvider.put("method", "OAuth1.0a"); // future use.
                serviceProvider.put("target", "https://api.cybozulive.com/api/notification/V2");
                serviceProvider.put("request", "https://api.cybozulive.com/oauth/initiate");
                serviceProvider.put("authorize", "https://api.cybozulive.com/oauth/authorize");
                serviceProvider.put("access", "https://api.cybozulive.com/oauth/token");
                service.setServiceProvider(serviceProvider.toString());
            }
            { // consumer token settings which is given by service provider.
                JSONObject token = new JSONObject();
                token.put("key", "<PUT-YOUR-CONSUMER-KEY>");
                token.put("secret", "<PUT-YOUR-CONSUMER-SECRET>");
                service.setConsumerToken(token.toString());
            }
            Datastore.put(service);
        }
        if (Datastore.query(meta).filter("serviceName", FilterOperator.EQUAL, "youRoom").count() == 0) {
            // not get started yet :(
            M_service service = new M_service();
            service.setServiceName("youRoom");
            { // service provider settings.
                // see <http://apidoc.youroom.in/authentication>
                // see <http://apidoc.youroom.in/rest-timeline-home>
                JSONObject serviceProvider = new JSONObject();
                serviceProvider.put("method", "OAuth1.0a"); // future use.
                serviceProvider.put("target", "https://www.youroom.in/");
                serviceProvider.put("request", "https://youroom.in/oauth/request_token");
                serviceProvider.put("authorize", "https://youroom.in/oauth/authorize");
                serviceProvider.put("access", "https://youroom.in/oauth/access_token");
                service.setServiceProvider(serviceProvider.toString());
            }
            { // consumer token settings which is given by service provider.
                JSONObject token = new JSONObject();
                token.put("key", "<PUT-YOUR-CONSUMER-KEY>");
                token.put("secret", "<PUT-YOUR-CONSUMER-SECRET>");
                service.setConsumerToken(token.toString());
            }
            Datastore.put(service);
        }
        return redirect("/");
    }
}
